import scrapy
import mysql.connector
from scrapy.utils.project import get_project_settings
import json
from shopSpider.items import GoodsDetailItem

settings = get_project_settings()
class GoodsDetailSpider(scrapy.Spider):
    name = "goods_detail"
    allowed_domains = ["api-gw.onebound.cn"]
    start_urls = ["https://api-gw.onebound.cn"]
    host = settings.get('MYSQL_HOST')
    port = settings.get('MYSQL_PORT')
    dbname = settings.get('MYSQL_DBNAME')
    user = settings.get('MYSQL_USER')
    password = settings.get('MYSQL_PASSWORD')

    def start_requests(self):
        self.conn = mysql.connector.connect(
            host=self.host,
            port=self.port,
            database=self.dbname,
            user=self.user,
            password=self.password,
        )
        self.cursor = self.conn.cursor()
        self.cursor.execute("SELECT num_iid from taobao_goods_list")
        self.result = self.cursor.fetchall()
        for url in self.result:
            num_iid = ''.join(url)
            url = "https://api-gw.onebound.cn/taobao/item_get/?key=t9911312388&&num_iid={}&is_promotion=1&&lang=zh-CN&secret=23883da6".format(num_iid)
            print(url)
            yield scrapy.Request(url, self.parse)

    def parse(self, response):
        data = json.loads(response.text)
        for goodsItem in data['item']:
            item = GoodsDetailItem()
            item['title'] = goodsItem['title']
            item['num_iid'] = goodsItem['num_iid']
            item['desc_short'] = goodsItem['desc_short']
            item['price'] = goodsItem['price']
            item['orginal_price'] = goodsItem['orginal_price']
            item['nick'] =  goodsItem['nick']
            item['num'] = goodsItem['num']
            item['pic_url']= goodsItem['pic_url']
            item['brand']= goodsItem['brand']
            item['brandId']= goodsItem['brandId']
            item['detail_url']= goodsItem['detail_url']
            item['desc']= goodsItem['desc']
            item['item_imgs']= goodsItem['item_imgs']
            item['prop_imgs']= goodsItem['prop_imgs']
            item['props']= goodsItem['props']
            item['props_name']= goodsItem['props_name']
            item['skus']= goodsItem['skus']
            item['props_list']= goodsItem['props_list']
            item['seller_info']= goodsItem['seller_info']
            item['seller_id']= goodsItem['seller_id']
            item['shop_id']= goodsItem['shop_id']
            yield item
